package wdstruct

import "sync"

type objectpool struct {
	maxcount int32
	call     I_Object_Pool
	pool     I_Deque_Hand
	autex    sync.Mutex
}

func NewObjectPool(max int32, objectcall I_Object_Pool) I_Object_Pool_Hand {
	pool := &objectpool{
		maxcount: max,
		call:     objectcall,
		pool:     New_Deque(),
		autex:    sync.Mutex{},
	}
	return pool
}

func (o *objectpool) Get() (obj interface{}) {
	o.autex.Lock()
	if o.pool.GetLength() > 0 {
		obj = o.pool.PullFromHead()
	} else {
		o.autex.Unlock()
		obj = o.call.New()
		return obj
	}
	o.autex.Unlock()
	return obj
}

func (o *objectpool) Give(obj interface{}) {
	if o.pool.GetLength() < o.maxcount {
		o.call.Recycle(obj)
		o.pool.PushToTail(obj)
	}
}

//-----------------------------默认池---------------------
